package com.houxm.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Pager implements Serializable {

    private static final long serialVersionUID = 2875790409419632498L;

    private static final int PAGE_DEFAULT_LIMIT = 20;
    
    private static final int PAGE_BAR_SIZE = 9;
    
    private int currentPage;

    private int totalCount;
    
    private int totalPage;
    
    private int pageLimit;
    
    private boolean hasNextPage;
    
    private boolean hasPrevPage;
    
    private List<String> pageBarList = new ArrayList<String>();

    public Pager(int currentPage) {
        this.currentPage = currentPage;
    }
    
    public void setTotalCount(int totalCount) {
        if (totalCount <= 0) {
            return;
        }
        this.totalCount = totalCount;

        if (this.pageLimit <= 0) {
            this.pageLimit = PAGE_DEFAULT_LIMIT;
        }

        if (this.totalCount % this.pageLimit == 0) {
            this.totalPage = this.totalCount / this.pageLimit;
        } else {
            this.totalPage = this.totalCount / this.pageLimit + 1;
        }

        if (this.currentPage > this.totalPage) {
            this.currentPage = this.totalPage;
        } else if (this.currentPage <= 0) {
            this.currentPage = 1;
        }

        this.setPageBar();

        this.hasPrevPage = this.currentPage != 1;
        this.hasNextPage = this.currentPage != this.totalPage;
        
    }

    public void setPageBar() {
        if (this.totalPage <= PAGE_BAR_SIZE) {
            for (int i = 1; i <= this.totalPage; i++) {
                pageBarList.add(Integer.toString(i));
            }
        } else if (this.currentPage <= PAGE_BAR_SIZE / 2) {
            for (int i = 1; i <= PAGE_BAR_SIZE; i++) {
                pageBarList.add(Integer.toString(i));
            }
        } else if (this.currentPage >= this.totalPage - (PAGE_BAR_SIZE - 1) / 2) {
            for (int i = this.totalPage - PAGE_BAR_SIZE + 1; i <= this.totalPage; i++) {
                pageBarList.add(Integer.toString(i));
            }
        } else {
            for (int i = this.currentPage - PAGE_BAR_SIZE / 2; i < this.currentPage - PAGE_BAR_SIZE / 2 + PAGE_BAR_SIZE; i++) {
                pageBarList.add(Integer.toString(i));
            }
        }
    }
    
    public int getNextPage() {
        return this.currentPage + 1;
    }

    public int getPrevPage() {
        return this.currentPage - 1;
    }
    
    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public boolean isHasPrevPage() {
        return hasPrevPage;
    }

    public void setHasPrevPage(boolean hasPrevPage) {
        this.hasPrevPage = hasPrevPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getPageLimit() {
        return pageLimit;
    }

    public void setPageLimit(int pageLimit) {
        this.pageLimit = pageLimit;
    }

    public List<String> getPageBarList() {
        return pageBarList;
    }

    public void setPageBarList(List<String> pageBarList) {
        this.pageBarList = pageBarList;
    }
}
